#!/usr/bin/env python 
import cherrypy
from genshi.core import Markup
from comm.proj import *
from comm.tool import *
from comm.pager import *
from comm.model.m_admin import *
from comm.biz.b_admin import *
from comm.error import ProjError

class WSGUser:
	def DoLogin(self, *argv, **kwds):
		uid = Web.get_with_default(kwds, 'uid' )
		pwd = Web.get_with_default(kwds, 'pwd' )
		back = Web.get_with_default(kwds, 'back', default='/admin' )
		ajax = Web.get_with_default(kwds, 'ajax', default=False )
		
		if not (uid and pwd):
			raise ProjError('plz. fill the uid')

		err = 'User doesnt exist. or wrong password'
		if 'admin'==uid: 
			if '123qweasd'!=pwd : raise ProjError(err)
		else:	
			user = BizAdmin().GetUser(uid)
			if not user or user.pwdhash!=Tool.md5(Tool.md5(pwd)): raise ProjError(err)

		cherrypy.session['uid']=uid
		raise cherrypy.HTTPRedirect(back)

		return '' 

	def login(self, *argv, **kwds):
		back_url = Web.get_with_default(kwds, 'back', default='/admin/user/list' )
		return Proj.HTML('admin/user_login.html', back_url=back_url) 
	
	def list(self, *argv, **kwds):
		session_uid = Web.check_login()
		try: page = int(kwds['page'])
		except: page = 1
		try: size = int(kwds['size'])
		except: size = 10
		
		(user_list, user_total) = BizAdmin().ListUser( (page-1)*size, size )

		pager = Pager(page_size=size, total=user_total, cur_page=page, cur_size=len(user_list) ) 
		main_set = pager.main_set() 
		
		return Proj.HTML('admin/user_list.html', session_uid=session_uid, user_list=user_list, page=page, main_set=main_set )

	def add(self, *argv, **kwds):
		session_uid = Web.check_login()
		return Proj.HTML('admin/user_add.html' ) 

	def edit(self, *argv, **kwds):
		session_uid = Web.check_login()
		uid = Web.get_with_default(kwds, 'uid' )
		#TODO:verify
		user = BizAdmin().GetUser(uid)
		return Proj.HTML('admin/user_edit.html', uid=user.uid) 
		
	def remove(self, *argv, **kwds):
		session_uid = Web.check_login()
		uid = Web.get_with_default(kwds, 'uid' )
		user = BizAdmin().GetUser(uid)
		return Proj.HTML('admin/user_remove.html', uid=user.uid) 
		
	def DoAdd(self, *argv, **kwds):
		session_uid = Web.check_login()
		try: uid = kwds['uid']
		except: uid = '' 
		try: pwd = kwds['pwd']
		except: pwd = ''
		try: ajax = bool(kwds['ajax'])
		except: ajax = False 
		
		BizAdmin().AddUser(uid, pwd)

		if ajax :
			return Proj.XMLSimple( result=0, msg=Markup('OK') )
		else:
			return Proj.HTML('admin/done.html', msg='successfully proceeded', color='green')

	def DoEdit(self, *argv, **kwds):
		session_uid = Web.check_login()
		uid = Web.get_with_default(kwds, 'uid' )
		pwd = Web.get_with_default(kwds, 'pwd' )
		ajax = Web.get_with_default(kwds, 'ajax' )
		if 'true'==ajax : ajax = True
		else: ajax = False
		
		BizAdmin().EditUser(uid, pwd)

		if ajax :
			return Proj.XMLSimple( result=0, msg=Markup('OK') )
		else:
			return Proj.HTML('admin/done.html', msg='successfully proceeded', color='green')

	def DoRemove(self, *argv, **kwds):
		session_uid = Web.check_login()
		uid = Web.get_with_default(kwds, 'uid' )
		try: ajax = bool(kwds['ajax'])
		except: ajax = False 
		
		BizAdmin().RemoveUser(uid)

		if ajax :
			return Proj.XMLSimple( result=0, msg=Markup('OK') )
		else:
			return Proj.HTML('admin/done.html', msg='successfully proceeded', color='green')
	
	login.exposed = True
	list.exposed = True
	add.exposed = True
	edit.exposed = True
	remove.exposed = True
	DoLogin.exposed = True
	DoAdd.exposed = True
	DoEdit.exposed = True
	DoRemove.exposed = True
	#set list as index 
	index = list 


